home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 2
/
Nebula Two.iso
/
Drivers
/
play3401
/
scsicommands.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-03-02
|
10KB
|
326 lines
/* -*-C-*-
*******************************************************************************
*
* File: scsicommands.c
* RCS: /usr/local/lib/cvs/play3401/scsicommands.c,v 1.2 1994/03/03 19:10:44 cedman Exp
* Description: Execute commands directly to the SCSI drive
* Author: Carl Edman
* Created: Sat May 8 23:07:42 1993
* Modified: Mon Jan 31 13:33:37 1994 (Carl Edman) cedman@capitalist.princeton.edu
* Language: C
* Package: N/A
* Status: Alpha
*
* (C) Copyright 1993, but otherwise this file is perfect freeware.
*
*******************************************************************************
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <libc.h>
#include <sys/file.h>
#include <sys/types.h>
#include <bsd/dev/scsireg.h>
#include <bsd/dev/disk.h>
#include "scsicommands.h"
static int to=0;
static int fd=-1;
void scsierror(struct scsi_req *srp)
{
int i;
i=1+1;
}
int scsigetdev(const char *id)
{
char name[1024];
int f,i;
struct drive_info di;
for(i=0;;i++)
{
sprintf(name,"/dev/rsd%dh",i);
if ((f=open(name,O_RDONLY))==-1)
{
fprintf(stderr,"No free generic SCSI device\n");
return -1;
}
if (ioctl(f,DKIOCINFO,&di)==-1)
{
fprintf(stderr,"Ioctl(DKIOCINFO): %s\n",strerror(errno));
return -1;
}
if (!strncmp(di.di_name,id,strlen(id))) break;
close(f);
}
fd=f;
return fd;
}
int scsisettimeout(int tm)
{
to=tm;
return to;
}
int scsiwrite6s(void *buf,int blen,u_char opcode,u_char lun,u_char spare,u_char opt,u_int len,u_char ctrl)
{
struct scsi_req srp;
bzero(&srp,sizeof(srp));
srp.sr_cdb.cdb_c6s.c6s_opcode=opcode;
srp.sr_cdb.cdb_c6s.c6s_lun=lun;
srp.sr_cdb.cdb_c6s.c6s_spare=spare;
srp.sr_cdb.cdb_c6s.c6s_opt=opt;
#if __BIG_ENDIAN__
srp.sr_cdb.cdb_c6s.c6s_len=len;
#elif __LITTLE_ENDIAN__
if (len > 0)
{
srp.sr_cdb.cdb_c6s.c6s_len0 = len & 0xFF;
srp.sr_cdb.cdb_c6s.c6s_len1 = (len >> 8) & 0xFF;
srp.sr_cdb.cdb_c6s.c6s_len2 = (len >> 16) & 0xFF;
}
else
srp.sr_cdb.cdb_c6s.c6s_len0 = srp.sr_cdb.cdb_c6s.c6s_len1 =
srp.sr_cdb.cdb_c6s.c6s_len2 = 0;
#endif
srp.sr_cdb.cdb_c6s.c6s_ctrl=ctrl;
srp.sr_addr=buf;
srp.sr_dma_max=blen;
srp.sr_dma_dir=SR_DMA_WR;
srp.sr_ioto=to;
ioctl(fd, SDIOCSRQ,&srp);
if (srp.sr_io_status!=0) { scsierror(&srp); return -1; }
return 0;
}
int scsiwrite10(void *buf,int blen,u_char opcode,u_char lun,u_char dp0,u_char fua,u_char mbz1,u_char reladr,u_int lba,u_char mbz2,u_short len,u_char ctrl)
{
struct scsi_req srp;
bzero(&srp,sizeof(srp));
srp.sr_cdb.cdb_c10.c10_opcode=opcode;
srp.sr_cdb.cdb_c10.c10_lun=lun;
srp.sr_cdb.cdb_c10.c10_dp0=dp0;
srp.sr_cdb.cdb_c10.c10_fua=fua;
srp.sr_cdb.cdb_c10.c10_mbz1=mbz1;
srp.sr_cdb.cdb_c10.c10_reladr=reladr;
#if __BIG_ENDIAN__
srp.sr_cdb.cdb_c10.c10_lba=lba;
#elif __LITTLE_ENDIAN__
if (lba > 0)
{
srp.sr_cdb.cdb_c10.c10_lba0 = lba & 0xFF;
srp.sr_cdb.cdb_c10.c10_lba1 = (lba >> 8) & 0xFF;
srp.sr_cdb.cdb_c10.c10_lba2 = (lba >> 16) & 0xFF;
srp.sr_cdb.cdb_c10.c10_lba3 = (lba >> 24) & 0xFF;
} else
srp.sr_cdb.cdb_c10.c10_lba0 = srp.sr_cdb.cdb_c10.c10_lba1
= srp.sr_cdb.cdb_c10.c10_lba2 = srp.sr_cdb.cdb_c10.c10_lba3 = 0;
#endif
srp.sr_cdb.cdb_c10.c10_mbz2=mbz2;
#if __BIG_ENDIAN__
srp.sr_cdb.cdb_c10.c10_len=len;
#elif __LITTLE_ENDIAN__
if (len > 0)
{
srp.sr_cdb.cdb_c10.c10_len0 = len & 0xFF;
srp.sr_cdb.cdb_c10.c10_len1 = (len >> 8) & 0xFF;
}
else
srp.sr_cdb.cdb_c10.c10_len0 = srp.sr_cdb.cdb_c10.c10_len1 = 0;
#endif
srp.sr_cdb.cdb_c10.c10_ctrl=ctrl;
srp.sr_addr=buf;
srp.sr_dma_max=blen;
srp.sr_dma_dir=SR_DMA_WR;
srp.sr_ioto=to;
ioctl(fd,SDIOCSRQ,&srp);
if (srp.sr_io_status!=0) { scsierror(&srp); return -1; }
return 0;
}
int scsiwrite12(void *buf,int blen,u_char opcode,u_char lun,u_char dp0,u_char fua,u_char mbz1,u_char reladr,u_int lba,u_char mbz2,u_char mbz3,u_char mbz4,u_short len,u_char ctrl)
{
struct scsi_req srp;
bzero(&srp,sizeof(srp));
srp.sr_cdb.cdb_c12.c12_opcode=opcode;
srp.sr_cdb.cdb_c12.c12_lun=lun;
srp.sr_cdb.cdb_c12.c12_dp0=dp0;
srp.sr_cdb.cdb_c12.c12_fua=fua;
srp.sr_cdb.cdb_c12.c12_mbz1=mbz1;
srp.sr_cdb.cdb_c12.c12_reladr=reladr;
#if __BIG_ENDIAN__
srp.sr_cdb.cdb_c12.c12_lba=lba;
#elif __LITTLE_ENDIAN__
if (lba > 0)
{
srp.sr_cdb.cdb_c12.c12_lba0 = lba & 0xFF;
srp.sr_cdb.cdb_c12.c12_lba1 = (lba >> 8) & 0xFF;
srp.sr_cdb.cdb_c12.c12_lba2 = (lba >> 16) & 0xFF;
srp.sr_cdb.cdb_c12.c12_lba3 = (lba >> 24) & 0xFF;
}
else
srp.sr_cdb.cdb_c12.c12_lba0 = srp.sr_cdb.cdb_c12.c12_lba1
= srp.sr_cdb.cdb_c12.c12_lba2 = srp.sr_cdb.cdb_c12.c12_lba3 = 0;
#endif
srp.sr_cdb.cdb_c12.c12_mbz2=mbz2;
srp.sr_cdb.cdb_c12.c12_mbz3=mbz3;
srp.sr_cdb.cdb_c12.c12_mbz4=mbz4;
#if __BIG_ENDIAN__
srp.sr_cdb.cdb_c12.c12_len=len;
#elif __LITTLE_ENDIAN__
if (len > 0)
{
srp.sr_cdb.cdb_c12.c12_len0 = len & 0xFF;
srp.sr_cdb.cdb_c12.c12_len1 = (len >> 8) & 0xFF;
}
else
srp.sr_cdb.cdb_c12.c12_len0 = srp.sr_cdb.cdb_c12.c12_len1 = 0;
#endif
srp.sr_cdb.cdb_c12.c12_ctrl=ctrl;
srp.sr_addr=buf;
srp.sr_dma_max=blen;
srp.sr_dma_dir=SR_DMA_WR;
srp.sr_ioto=to;
ioctl(fd,SDIOCSRQ,&srp);
if (srp.sr_io_status!=0) { scsierror(&srp); return -1; }
return 0;
}
int scsiread6s(void *buf,int blen,u_char opcode,u_char lun,u_char spare,u_char opt,u_int len,u_char ctrl)
{
struct scsi_req srp;
bzero(&srp,sizeof(srp));
srp.sr_cdb.cdb_c6s.c6s_opcode=opcode;
srp.sr_cdb.cdb_c6s.c6s_lun=lun;
srp.sr_cdb.cdb_c6s.c6s_spare=spare;
srp.sr_cdb.cdb_c6s.c6s_opt=opt;
#if __BIG_ENDIAN__
srp.sr_cdb.cdb_c6s.c6s_len=len;
#elif __LITTLE_ENDIAN__
if (len > 0)
{
srp.sr_cdb.cdb_c6s.c6s_len0 = len & 0xFF;
srp.sr_cdb.cdb_c6s.c6s_len1 = (len >> 8) & 0xFF;
srp.sr_cdb.cdb_c6s.c6s_len2 = (len >> 16) & 0xFF;
}
else
srp.sr_cdb.cdb_c6s.c6s_len0 = srp.sr_cdb.cdb_c6s.c6s_len1
= srp.sr_cdb.cdb_c6s.c6s_len2 = 0;
#endif
srp.sr_cdb.cdb_c6s.c6s_ctrl=ctrl;
srp.sr_addr=buf;
srp.sr_dma_max=blen;
srp.sr_dma_dir=SR_DMA_RD;
srp.sr_ioto=to;
ioctl(fd,SDIOCSRQ,&srp);
if (srp.sr_io_status!=0) { scsierror(&srp); return -1; }
return 0;
}
int scsiread10(void *buf,int blen,u_char opcode,u_char lun,u_char dp0,u_char fua,u_char mbz1,u_char reladr,u_int lba,u_char mbz2,u_short len,u_char ctrl)
{
struct scsi_req srp;
bzero(&srp,sizeof(srp));
srp.sr_cdb.cdb_c10.c10_opcode=opcode;
srp.sr_cdb.cdb_c10.c10_lun=lun;
srp.sr_cdb.cdb_c10.c10_dp0=dp0;
srp.sr_cdb.cdb_c10.c10_fua=fua;
srp.sr_cdb.cdb_c10.c10_mbz1=mbz1;
srp.sr_cdb.cdb_c10.c10_reladr=reladr;
#if __BIG_ENDIAN__
srp.sr_cdb.cdb_c10.c10_lba=lba;
#elif __LITTLE_ENDIAN__
if (lba > 0)
{
srp.sr_cdb.cdb_c10.c10_lba0 = lba & 0xFF;
srp.sr_cdb.cdb_c10.c10_lba1 = (lba >> 8) & 0xFF;
srp.sr_cdb.cdb_c10.c10_lba2 = (lba >> 16) & 0xFF;
srp.sr_cdb.cdb_c10.c10_lba3 = (lba >> 24) & 0xFF;
}
else
srp.sr_cdb.cdb_c10.c10_lba0 = srp.sr_cdb.cdb_c10.c10_lba1
= srp.sr_cdb.cdb_c10.c10_lba2 = srp.sr_cdb.cdb_c10.c10_lba3 = 0;
#endif
srp.sr_cdb.cdb_c10.c10_mbz2=mbz2;
#if __BIG_ENDIAN__
srp.sr_cdb.cdb_c10.c10_len=len;
#elif __LITTLE_ENDIAN__
if (len > 0)
{
srp.sr_cdb.cdb_c10.c10_len0 = len & 0xFF;
srp.sr_cdb.cdb_c10.c10_len1 = (len >> 8) & 0xFF;
}
else
srp.sr_cdb.cdb_c10.c10_len0 = srp.sr_cdb.cdb_c10.c10_len1 = 0;
#endif
srp.sr_cdb.cdb_c10.c10_ctrl=ctrl;
srp.sr_addr=buf;
srp.sr_dma_max=blen;
srp.sr_dma_dir=SR_DMA_RD;
srp.sr_ioto=to;
ioctl(fd,SDIOCSRQ,&srp);
if (srp.sr_io_status!=0) { scsierror(&srp); return -1; }
return 0;
}
int scsiread12(void *buf,int blen,u_char opcode,u_char lun,u_char dp0,u_char fua,u_char mbz1,u_char reladr,u_int lba,u_char mbz2,u_char mbz3,u_char mbz4,u_short len,u_char ctrl)
{
struct scsi_req srp;
bzero(&srp,sizeof(srp));
srp.sr_cdb.cdb_c12.c12_opcode=opcode;
srp.sr_cdb.cdb_c12.c12_lun=lun;
srp.sr_cdb.cdb_c12.c12_dp0=dp0;
srp.sr_cdb.cdb_c12.c12_fua=fua;
srp.sr_cdb.cdb_c12.c12_mbz1=mbz1;
srp.sr_cdb.cdb_c12.c12_reladr=reladr;
#if __BIG_ENDIAN__
srp.sr_cdb.cdb_c12.c12_lba=lba;
#elif __LITTLE_ENDIAN__
if (lba > 0)
{
srp.sr_cdb.cdb_c12.c12_lba0 = lba & 0xFF;
srp.sr_cdb.cdb_c12.c12_lba1 = (lba >> 8) & 0xFF;
srp.sr_cdb.cdb_c12.c12_lba2 = (lba >> 16) & 0xFF;
srp.sr_cdb.cdb_c12.c12_lba3 = (lba >> 24) & 0xFF;
}
else
srp.sr_cdb.cdb_c12.c12_lba0 = srp.sr_cdb.cdb_c12.c12_lba1
= srp.sr_cdb.cdb_c12.c12_lba2 = srp.sr_cdb.cdb_c12.c12_lba3 = 0;
#endif
srp.sr_cdb.cdb_c12.c12_mbz2=mbz2;
srp.sr_cdb.cdb_c12.c12_mbz3=mbz3;
srp.sr_cdb.cdb_c12.c12_mbz4=mbz4;
#if __BIG_ENDIAN__
srp.sr_cdb.cdb_c12.c12_len=len;
#elif __LITTLE_ENDIAN__
if (len > 0)
{
srp.sr_cdb.cdb_c12.c12_len0 = len & 0xFF;
srp.sr_cdb.cdb_c12.c12_len1 = (len >> 8) & 0xFF;
}
else
srp.sr_cdb.cdb_c12.c12_len0 = srp.sr_cdb.cdb_c12.c12_len1 = 0;
#endif
srp.sr_cdb.cdb_c12.c12_ctrl=ctrl;
srp.sr_addr=buf;
srp.sr_dma_max=blen;
srp.sr_dma_dir=SR_DMA_WR;
srp.sr_ioto=to;
ioctl(fd,SDIOCSRQ,&srp);
if (srp.sr_io_status!=0) { scsierror(&srp); return -1; }
return 0;
}
int scsiclose(int eject)
{
int ret=0;
if (eject && ioctl(fd,DKIOCEJECT,0)==-1) ret=1;
close(fd);
fd=-1;
return ret;
}